import 'dart:math';

// Future<String?> showFolderSelector(BuildContext context,
//     {List<String>? allowedExtensions,
//     FilesystemType fsType = FilesystemType.all,
//     String title = "请选择",
//     String rootName = "我的电脑",
//     bool showNewFolderButton = false}) async {
//   final repository = DisksRepository();
//   final List<Disk> disks = await repository.query;

//   List<FilesystemPickerShortcut> shortcuts = [];
//   for (final Disk disk in disks) {
//     final List<Mountpoint> mountpoints = disk.mountpoints;
//     for (final Mountpoint mountpoint in mountpoints) {
//       shortcuts.add(FilesystemPickerShortcut(
//           name: mountpoint.label == null || mountpoint.label!.isEmpty
//               ? mountpoint.path
//               : mountpoint.label!,
//           path: Directory(mountpoint.path)));
//     }
//   }
//   if (context.mounted) {
//     String? selectedFolder = await FilesystemPicker.openDialog(
//         title: title,
//         context: context,
//         rootName: rootName,
//         shortcuts: shortcuts,
//         showGoUp: true,
//         theme: const FilesystemPickerTheme(
//             contextActions: FilesystemPickerContextActionsThemeData(
//                 buttonTheme: FilesystemPickerContextActionsButtonThemeData(
//                     iconTheme: IconThemeData(color: Colors.black)))),
//         contextActions: [
//           if (showNewFolderButton == true)
//             FilesystemPickerNewFolderContextAction(
//                 text: "新建文件夹",
//                 dialogTitle: "新建文件夹",
//                 dialogCancelText: "取消",
//                 dialogOkText: "确定",
//                 dialogPrompt: "请输入新文件夹名称")
//         ],
//         fsType: fsType,
//         allowedExtensions: allowedExtensions);
//     return selectedFolder;
//   }
//   return null;
// }

String getFileSizeString({required int bytes, int decimals = 1}) {
  const suffixes = ["B", "KB", "MB", "GB", "TB"];
  if (bytes == 0) return '0${suffixes[0]}';
  var i = (log(bytes) / log(1024)).floor();
  return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + suffixes[i];
}

String getFileExt(String fileName) {
  List<String> parts = fileName.split(".");
  return parts.length >= 2 ? parts[parts.length - 1] : "";
}

String getFileTitle(String fileName) {
  String ext = getFileExt(fileName);
  return fileName.substring(0, fileName.length - ext.length);
}

String replaceTitleInPath(String fullPath, String newTitle) {
  String pathSeperator = "\\";
  if (!fullPath.contains(pathSeperator)) {
    pathSeperator = "/";
  }

  List<String> parts = fullPath.split(pathSeperator);
  String fileName = parts[parts.length - 1];
  String ext = getFileExt(fileName);
  fileName = "$newTitle.$ext";
  parts[parts.length - 1] = fileName;
  return parts.join(pathSeperator);
}
